package io.renren.framework.session;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 会话工具类
 */
public class SessionUtil {
	private static ThreadLocal<Map<String, Object>> THREAD_LOCAL_SESSION = new ThreadLocal<Map<String, Object>>() {
		@Override
		protected Map<String, Object> initialValue() {
			return new HashMap<String, Object>();
		}
	};

	public static Object getCrmUser() {
		return THREAD_LOCAL_SESSION.get().get("crmUser");
	}

	public static void putCrmUser(Object crmUser) {
		THREAD_LOCAL_SESSION.get().put("crmUser", crmUser);
	}

	public static void putCrmUserOper(Set<String> agentUserOperSet) {
		THREAD_LOCAL_SESSION.get().put("crmUserOper", agentUserOperSet);
	}

	public static boolean containsCrmUserOper(String oper) {
		return getCrmUserOper().contains(oper);
	}

	@SuppressWarnings("unchecked")
	public static Set<String> getCrmUserOper() {
		Set<String> crmUserOperSet = (Set<String>) THREAD_LOCAL_SESSION.get().get("crmUserOper");
		crmUserOperSet = crmUserOperSet == null ? new HashSet<>() : crmUserOperSet;
		return crmUserOperSet;
	}

	public static Object getAppUser() {
		return THREAD_LOCAL_SESSION.get().get("appUser");
	}

	public static void putAppUser(Object user) {
		THREAD_LOCAL_SESSION.get().put("appUser", user);
	}

	public static void clear() {
		THREAD_LOCAL_SESSION.get().clear();
	}
}
